/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};
*/
class Solution {
public:
    vector<vector<int> > Print(TreeNode* pRoot) {
    	vector<vector<int>> output;
    	if(!pRoot)
    		return output;
    	queue.push_back(pRoot);
    	int cnt = 1;
    	while(!queue.empty())
    	{
    		vector<int> line;
    		int size = 0;
    		while(cnt > 0)
    		{
    			TreeNode * p = queue.front();
    			queue.pop_front();
    			--cnt;
    			line.push_back(p->val);
    			if(p->left)
    			{
    				queue.push_back(p->left);
    				++size;
    			}
    			if(p->right)
    			{
    				queue.push_back(p->right);
    				++size;
    			}
    		}
    		output.push_back(line);
    		cnt = size;
    	}

    	return output;
    }
private:
	deque<TreeNode *> queue;
};